class Heap(object):
    def __init__(self):
        self.vals = []

    def __len__(self):
        return len(self.vals)

    def set_heap(self):
        self.build_heap()

    def head_adjust(self,k):
        v_len = len(self.vals)
        self.vals[0] = self.vals[k]
        i = 2*k
        while i <= v_len:
            if i < v_len and self.vals[i] < self.vals[i+1]:
                i += 1
            if self.vals[0] >= self.vals[i]:
                break
            else:
                self.vals[k] = self.vals[i]
                k = i
            i *= 2
        self.vals[k] = self.vals[0]

    def build_heap(self):
        for i in range(int(len(self.vals)/2),-1,-1):
            self.head_adjust(i)

    def push(self,e):
        self.vals.append(e)
        self.set_heap()

    def pop(self):
        e = self.vals.pop()
        self.set_heap()
        return e



if __name__ == "__main__":
    def head_adjust(test,k):
        test[0] = test[k]
        i = 2*k
        while i<=len(test):
            if i<len(test) and test[i]<test[i+1]:
                i += 1
            if test[0]>test[i]: break

            else:
                test[k] = test[i]
                k = i
        test[k] = test[0]

    def build_heap(test):
        for i in range(int(len(test)/2),-1,-1):
            head_adjust(test,i)


